#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#ifndef N
#error undefined N
#else

int C[N][N];
int A[N][N];
int B[N][N];

#ifndef DVIB
#define DVIB 0
#endif

#define Bsize (N>>DIVB)
//int BlA[Bsize][Bsize];
//int BlB[Bsize][Bsize];
//int BlC[Bsize][Bsize];

void mult_ijk ()
{
  int i,j,k;

  for ( i=0 ; i < N; i++ )
    for ( j=0 ; j < N; j++ )
      for ( k=0 ; k < N ; k++ )
         C[i][j]+= A[i][k]*B[k][j];
}

void mult_ikj ()
{
  int i,j,k;

  for ( i=0 ; i < N; i++ )
    for ( k=0 ; k < N ; k++ )
      for ( j=0 ; j < N; j++ )
         C[i][j]+= A[i][k]*B[k][j];
}

void mult_jik ()
{
  int i,j,k;

  for ( j=0 ; j < N; j++ )
    for ( i=0 ; i < N; i++ )
      for ( k=0 ; k < N ; k++ )
         C[i][j]+= A[i][k]*B[k][j];
}

void mult_jki ()
{
  int i,j,k;

  for ( j=0 ; j < N; j++ )
    for ( k=0 ; k < N ; k++ )
      for ( i=0 ; i < N; i++ )
         C[i][j]+= A[i][k]*B[k][j];
}

void mult_kij ()
{
  int i,j,k;

  for ( k=0 ; k < N ; k++ )
    for ( i=0 ; i < N; i++ )
      for ( j=0 ; j < N; j++ )
         C[i][j]+= A[i][k]*B[k][j];
}

void mult_kji ()
{
  int i,j,k;

  for ( k=0 ; k < N ; k++ )
    for ( j=0 ; j < N; j++ )
      for ( i=0 ; i < N; i++ )
         C[i][j]+= A[i][k]*B[k][j];
}

void mult_ijkij ()
{
  int i,j,k;
  int ii, jj, kk;

  for (kk=0 ; kk < N ; kk += Bsize)
    for (jj=0 ; jj < N ; jj += Bsize)
      for ( i=0 ; i < N ; i++ )
        for ( k=kk ; k < (kk+Bsize) ; k++ )
          for ( j=jj ; j < (jj+Bsize) ; j++ )
            C[i][j]+= A[i][k]*B[k][j];
}

void init(int M[N][N])
{
  int i,j;

  for (i=0; i<N; i++)
    for (j=0; j<N; j++)
      M[i][j] = i+j;

}

int main(int argc, char *argv[])
{
  init(A);
  init(B);
  memset(C, 0, sizeof(C));

#ifdef IJK
  mult_ijk();
#endif
#ifdef IKJ
  mult_ikj();
#endif
#ifdef JIK
  mult_jik();
#endif
#ifdef JKI
  mult_jki();
#endif
#ifdef KIJ
  mult_kij();
#endif
#ifdef KJI
  mult_kji();
#endif
#ifdef IJKIJ
  mult_ijkij();
#endif

  if (write(1, C, sizeof(C)) < 0) exit(1);

  return 0;
}
#endif
